package xrz.cuanbao.views;

import org.xutils.x;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import uk.co.senab.photoview.PhotoView;
import xrz.cuanbao.R;

public class LoadImageView extends LinearLayout {

	private ImageView m_ImageView;
	private ProgressBar m_Progress;
	private LayoutParams params;
	private boolean isPhotoView;
	private PhotoView photoView;

	public LoadImageView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public LoadImageView(Context context,boolean isPhotoView) {
		super(context);
		this.isPhotoView = isPhotoView;
		init();
	}

	private void init() {
		setOrientation(VERTICAL);
		m_Progress = (ProgressBar) LayoutInflater.from(getContext()).inflate(
				R.layout.progressbar, null);
		
		
		if(isPhotoView){
			setGravity(Gravity.CENTER_VERTICAL);
			params = new LayoutParams(LayoutParams.MATCH_PARENT,
					LayoutParams.WRAP_CONTENT);
			photoView = new PhotoView(getContext());
			photoView.setMinimumHeight(300);
			photoView.setLayoutParams(params);
		}else{
			params = new LayoutParams(LayoutParams.MATCH_PARENT,
					LayoutParams.MATCH_PARENT);
			m_ImageView = new ImageView(getContext());
			m_ImageView.setLayoutParams(params);
			m_ImageView.setScaleType(ScaleType.FIT_XY);
		}
		
		
		params = new LayoutParams(LayoutParams.MATCH_PARENT, 3);
		addView(m_Progress, params);
		if(isPhotoView){
			addView(photoView);
		}else{
			addView(m_ImageView);
		}
	}

	public void loadImage(String url) {
		if(isPhotoView){
			x.image().bind(photoView, url);
		}else{
			x.image().bind(m_ImageView, url);
		}
	}

}
